package org.zxl.iotest.original.blocking;

import java.io.IOException;
import java.net.*;

public class DatagramSocket1 {
    public static void main(String[] args) throws SocketException, UnknownHostException , IOException {
        //创建socket，绑定端口1234
        DatagramSocket zhang = new DatagramSocket(1234);
        System.out.println("zhang ready!!!!");
        byte[] bytes = "I am zhang, what is your name? please tell me.".getBytes();
        DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length, InetAddress.getLocalHost(), 4321);
        zhang.send(datagramPacket);
        //接受数据
        zhang.receive(datagramPacket);//阻塞动作，一直等待返回
        //从返回数据报包中取数据
        byte[] data = datagramPacket.getData();
        int length = datagramPacket.getLength();

        String s = new String(data ,0 ,length);
        System.out.println("the zeng 给你发来消息，内容是： \r\n"+s);
    }
}
